<template>
  <VPage>
    <VTable ref="tableRef" v-bind="tableOptions">
      <template #form="{ form }">
        <VCascader v-model="form.department_name" placeholder="部门" type="department" multiple collapse-tags filterable clearable checkStrictly style="min-width: 160px" />
        <VCascader v-model="form.update_user_id" placeholder="更新人" multiple collapse-tags filterable clearable
          style="min-width: 160px" />
        <VGroup>
          <div class="v-group-item">更新时间</div>
          <VDate v-model="form.dateRange.value" />
        </VGroup>
      </template>
      <template #toolbar_btns>
        <VButton auth="/purchase/settings/department/add" type="primary" @click="create(tableRef)"><i class="dk-iconfont icon-Plus" />添加</VButton>
      </template>
    </VTable>
    <Add ref="addRef" :options="options" @success="query" />
    <VLog ref="logRef" />
  </VPage>
</template>
  
<script setup lang="jsx" name="Account">
import api from './api'
import Add from './components/Add.vue'
import { getOption } from '@/api/public'
import { ElMessage, ElMessageBox } from 'element-plus'

const tableRef = ref()
const logRef = ref()
const loadings = reactive({})

// 查询
const query = () => {
  tableRef?.value.query()
}

// 新增
const addRef = ref()
const create = () => {
  addRef?.value.open()
}

// 删除
const remove = (dc_id, rowIndex) => {
  ElMessageBox.confirm('此操作将删除该配置, 是否继续？', '提示', {
    confirmButtonClass: 'messagebox-confirm-button',
    confirmButtonText: '确定',
    cancelButtonText: '取消',
  })
    .then(() => {
      loadings['remove' + rowIndex] = true
      api.delete({ dc_ids: [dc_id] }).then(res => {
        query()
        ElMessage({ type: 'success', message: '删除成功!' })
      }).finally(() => {
        loadings['remove' + rowIndex] = false
      })
    })
    .catch(() => {
      ElMessage({ type: 'info', message: '取消操作', })
    })
}

// 获取options
const options = ref({})
getOption({ type: 'department_config' }).then(res => {
  options.value = res.data
})

// 列表配置
const tableOptions = reactive({
  showOverflow: false,
  formConfig: {
    data: {
      dateRange: { type: 'update_time', value: [], range: true },
    }
  },
  columns: [
    { field: 'department_name', title: '部门', minWidth: 120, line: 2 },
    { field: 'order_day', title: '下单时效', minWidth: 120, line: 2 },
    { field: 'update_user', title: '更新人', minWidth: 120, line: 2 },
    { field: 'update_time', title: '更新时间', minWidth: 120, line: 2 },
    {
      title: '操作', width: 150, fixed: 'right', slots: {
        default: ({ row, rowIndex }) => {
          const { dc_id } = row
          return <div class='nm flex gap-2'>
            <VButton auth="/purchase/settings/department/edit" link type="primary" onClick={() => addRef?.value.open(dc_id)}>编辑</VButton>
            <VButton auth="/purchase/settings/department/delete" link type="danger" loading={loadings['remove' + rowIndex]} onClick={() => remove(dc_id, rowIndex)}>删除</VButton>
            <VButton auth="/purchase/settings/department/log" link type="primary" onClick={() => logRef?.value.open({ log_type: 'department_config', dc_id })}>日志</VButton>
          </div>
        }
      }
    },
  ],
  proxyConfig: {
    autoLoad: false,
    ajax: {
      query: ({ page, form }) => api.query({ ...form, ...page })
    }
  }
})
</script>
  